package com.test.deviceinfo.util;


import cn.hutool.core.lang.Console;
import oshi.SystemInfo;
import oshi.hardware.ComputerSystem;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem;

import java.net.NetworkInterface;
import java.util.Enumeration;

public abstract class AbstractServer {

    private SystemInfo systemInfo = new SystemInfo();
    private HardwareAbstractionLayer hardware = systemInfo.getHardware();

    public abstract String getCPUSerial() throws Exception;

    public String getMainBoardSerial(){
        ComputerSystem computer = hardware.getComputerSystem();
//        Console.log("主板型号：{}", computer.getModel());
//        Console.log("主板序列号：{}", computer.getSerialNumber());

        return computer.getSerialNumber();
    }

    public String getMacByInetAddress() throws Exception{
        StringBuilder sb = new StringBuilder();
        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        byte[] mac = null;
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = allNetInterfaces.nextElement();
            if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint() || !netInterface.isUp()) {
                continue;
            }

            mac = netInterface.getHardwareAddress();
            if (mac != null) {
                for (int i = 0; i < mac.length; i++) {
                    sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "\n"));
                }
            }

        }

        return sb.toString();
    }
}
